package com.zxl.live.screen.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.zxl.live.screen.R;
import com.zxl.live.screen.call.helper.ThemeHelper;
import com.zxl.live.screen.call.model.ThemeModel;
import com.zxl.live.screen.call.model.bean.ThemeBean;
import com.zxl.live.screen.call.ui.activity.CallThemeSettingActivity;
import com.zxl.live.screen.call.ui.widget.CallRecycleView;
import com.zxl.live.screen.ui.activity.FeedbackActivity;
import com.zxl.live.screen.ui.receiver.NetworkConnectChangedReceiver;
import com.zxl.live.screen.ui.widget.WallpaperLoadErrorWidget;
import com.zxl.live.screen.ui.widget.WallpaperView;
import com.zxl.live.tools.base.BaseAppCompatFragment;
import com.zxl.live.tools.common.Observer;
import com.zxl.live.tools.track.StatisticsConstant;
import com.zxl.live.tools.track.StatisticsType;

import java.util.List;

/**
 * Created by Administrator on 2018/2/11.
 */

public class CallThemeFragment extends BaseAppCompatFragment implements View.OnClickListener, WallpaperLoadErrorWidget.OnLoadErrorListener, Observer<ThemeBean,Integer,List<ThemeBean>> {

    private boolean mIsJumNetSetting;

    private ThemeModel themeModel;

    private WallpaperView mLoadingView;

    private CallRecycleView mCallRecycleView;

    private WallpaperLoadErrorWidget mErrorView;

    private NetworkConnectChangedReceiver mReceiver = new NetworkConnectChangedReceiver() {

        @Override
        public void onNetWorkAvailable() {
            if (mErrorView.getVisibility() == View.VISIBLE && mErrorView.isNetWorkError()) {
                mErrorView.setVisibility(View.GONE);
                themeModel.startDataHandle();
                StatisticsType.LOAD_ERROR.upload(getContext(), StatisticsType.CALL_FLASH.getName(), StatisticsConstant.Event.NET_SUCCESS);
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_call_theme, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mReceiver.registerReceiver(getContext());
        mLoadingView = (WallpaperView) view.findViewById(R.id.wallpaper_loading);
        mCallRecycleView = (CallRecycleView) view.findViewById(R.id.recycler_view);
        mErrorView = (WallpaperLoadErrorWidget) view.findViewById(R.id.widget_load_error);
        mErrorView.setOnLoadErrorListener(this);

        themeModel = new ThemeModel();
        themeModel.registerObserver(this);
        themeModel.startDataHandle();
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        if (!TextUtils.isEmpty(ThemeHelper.flashPath)) {
            inflater.inflate(R.menu.menu_setting, menu);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        startActivity(new Intent(getContext(), CallThemeSettingActivity.class));
        StatisticsType.CALL_FLASH.upload(getContext(), StatisticsConstant.Event.STATUS, "click_detail");
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mIsJumNetSetting) {
            mIsJumNetSetting = false;
            themeModel.startDataHandle();
        }
        mCallRecycleView.refreshData();
        getAppCompatActivity().invalidateOptionsMenu();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mReceiver.unregisterReceiver(getContext());
    }

    @Override
    public void onFail(Integer errorCode) {
        if (errorCode == ThemeBean.ERROR_REQUEST_NET) {
            mErrorView.setType(WallpaperLoadErrorWidget.ERROR_REQUEST_NET);
        }
    }

    @Override
    public void onComplete(List<ThemeBean> themeBeen) {
        mLoadingView.setVisibility(View.GONE);
        mCallRecycleView.setList(themeBeen);
    }

    @Override
    public void onSuccess(ThemeBean themeBean) {
    }

    @Override
    public void onClick(View v) {
        onBackPressed();
    }

    @Override
    public void onSetNetWorkClick() {
        try {
            startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
            StatisticsType.LOAD_ERROR.upload(getActivity(), StatisticsType.CALL_FLASH.getName(), StatisticsConstant.Event.CLICK_NET);
            mIsJumNetSetting = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRetryClick() {
        themeModel.startDataHandle();
        StatisticsType.LOAD_ERROR.upload(getActivity(), StatisticsType.CALL_FLASH.getName(), StatisticsConstant.Event.CLICK_RETRY);
    }

    @Override
    public void onDownloadManagerClick() {
//        DownloadManagerActivity.startWallpaperPage(this);
        StatisticsType.LOAD_ERROR.upload(getActivity(), StatisticsType.CALL_FLASH.getName(), StatisticsConstant.Event.CLICK_DOWNLOAD);
    }

    @Override
    public void onFeedbackClick() {
        startActivity(new Intent(getContext(), FeedbackActivity.class));
        StatisticsType.LOAD_ERROR.upload(getActivity(), StatisticsType.CALL_FLASH.getName(), StatisticsConstant.Event.CLICK_FEEDBACK);
    }
}
